var utils = require('../../utils/util.js');
var config = require('../../base.conf.js').config;
var app = getApp();
Page({
  data: {
    url: "",
    title: '',
    preUrl: ""
  },

  onLoad: function(options) {
    var that = this
    var url = this.getUrl(decodeURIComponent(options.url) || decodeURIComponent(options.scene || ''));
    console.log(options)
    this.setData({
      url: 'https://www.pl298.com/' + `${url}`,
      preUrl: options.url,
      title: options.title || ''
    }, () => {
      wx.setNavigationBarTitle({
        title: that.data.title || '邻里邻外'
      })
    })
    //阿拉丁统计
    if (!config.isDebug) {
      app.aldstat.sendEvent('h5链接访问次数', {
        h5Url: decodeURIComponent(that.data.preUrl),
        nTime: utils.formatTime(new Date)
      });
    }
  },
  onShow: function () {
    
  },
  onUnload: function() {
    var pages = getCurrentPages();
    var mshpayorwx = pages[pages.length - 2]; //上一个界面
  },
  getUrl(url) {
    var pages = getCurrentPages();
    url = url.replace("[t]", +new Date()); //强制置刷新策略
    if (url.lastIndexOf("uid") > 0) {
      return url;
    }
    var userInfo = utils.getUserInfo();
    if (userInfo && userInfo.id) {
      var hashIndex = url.lastIndexOf("#");
      var queryIndex = url.lastIndexOf("?");
      // if (hashIndex > -1 && queryIndex > -1) {
      //   url = url.substring(0, queryIndex + 1) + "newXCX=1&" + url.substring(queryIndex + 1, url.length);
      // } else if (queryIndex > -1) {
      //   url += "&pages=" + pages.length + "&newXCX=1";
      // } else if (hashIndex > -1) {
      //   url += "?pages=" + pages.length + "&newXCX=1";
      // } else {
      //   url += "?pages=" + pages.length + "&newXCX=1";
      // }
    }
    return url;
  },
  logincallback() {
    var userInfo = utils.getUserInfo();
    if (userInfo && userInfo.id) {
      var url = this.getUrl(this.data.preUrl);
      console.log(url);
      this.setData({
        url: decodeURIComponent(url)
      })
    }
  },
  onShareAppMessage(options) {
    var path = "pages/mywebview/mywebview?url=" + this.data.preUrl;
    var {
      url
    } = this.data;
    var title = ""
    var webUrl = this.data.preUrl.split("?");
    // console.log(webUrl[0])
    if (webUrl[0] =='portal/active_101.html'){
      title ='水果卖身撩妹，全部底价，快围观啊！'
    }
    return {
      title: title || "邻里邻外" ,
      path: path
    }
  },
  bindGetMsg(e) {
    console.log(e.detail)
  }
})